package com.lagou.client;

import com.lagou.service.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.Callable;

/**
 * 用户客户端处理类
 * @author yzh
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext context;

    private String result;

    private Object para;

    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 接收响应的方法
     * @param ctx
     * @param msg
     */
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        RpcResponse rpcResponse = (RpcResponse)msg;
        result = rpcResponse.getResult()+"";
        notify();
    }

    /**
     * 发送调用请求的方法
     * @return
     * @throws InterruptedException
     */
    public synchronized Object call() throws InterruptedException {
        context.writeAndFlush(para);
        wait();
        return result;
    }

    public void setPara(Object para) {
        this.para = para;
    }

}
